package top.infopub.mgc.util;


import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;

import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.SystemUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.xhtmlrenderer.pdf.ITextRenderer;
import org.xhtmlrenderer.pdf.PDFEncryption;

import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfWriter;


/**
 * pdf工具
 * @author Awoke
 * @version 2018年11月5日
 * @see PdfUtils
 * @since
 */
public final class PdfUtils {

    private static final Logger logger = LoggerFactory.getLogger(PdfUtils.class);


    /**
     * 生成pdf文件  pdf不使用密码加密
     * @param pdfContent pdf内容
     * @param outputFile pdf输出文件路径
     * @return 
     * @see
     */
    public static boolean generateToFile(String pdfContent, String outputFile) {
        return generateToFile(pdfContent, outputFile, "");
    }

    /**
     * 生成pdf文件
     * @param pdfContent pdf内容
     * @param outputFile pdf输出文件路径
     * @param password pdf加密密码
     * @return 
     * @see
     */
    public static boolean generateToFile(String pdfContent, String outputFile, String password) {
        OutputStream out = null;
        try {
            ITextRenderer render = new ITextRenderer();
            // 添加字体，以支持中文
            // 注意：html协议中汉字字体 font-family:"SimSun"  
            // 控制pdf打印大小
            // @page {
            // size:290mm 350mm
            // }
            render.getFontResolver().addFont(
                SystemUtils.IS_OS_WINDOWS ? "C:/Windows/Fonts/SIMSUN.TTC" : "/usr/local/lib/SIMSUNB_linux.ttf",
                BaseFont.IDENTITY_H, BaseFont.NOT_EMBEDDED);

            if (!StringUtils.isEmpty(password)) {
                PDFEncryption encrypt = new PDFEncryption(password.getBytes(),
                    password.getBytes(), PdfWriter.ALLOW_SCREENREADERS);
                render.setPDFEncryption(encrypt);
            }
            File f = new File(outputFile);
            File parent = f.getParentFile();
            if (!parent.exists()) {
                parent.mkdirs();
            }
            out = new FileOutputStream(outputFile);

            render.setDocumentFromString(pdfContent);
            render.layout();
            render.createPDF(out);
            render.finishPDF();
            return true;
        }
        catch (Exception e) {
            logger.error("生成pdf失败", e);
        }
        finally {
            IOUtils.closeQuietly(out);
        }
        return false;
    }

    /**
     * Test
     * @param args 
     * @see
     */
    public static void main(String[] args) {
        try {
            String str = (FileUtils.readFileToString(new File("D:\\pdf\\ss.txt"), "GBK"));
            generateToFile(str, "D:\\pdf\\ss.pdf", "");
        }
        catch (IOException e) {
            e.printStackTrace();
        }
        catch (Exception e) {
            e.printStackTrace();
        }

    }
}
